﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Windows.Data;

namespace Soapi.Contrib.ValueConverters
{
    public class ParametersConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
            {
                return null;
            }
            return string.Join(";", ((IEnumerable<int>)value).Select(v => v.ToString()).ToArray());
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
            {
                return new List<int>();
            }
            return
                ((string)value).Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries).Select(
                    v => System.Convert.ToInt32(v)).ToList();
        }

        #endregion
    }
}